package com.yc.upload;

import com.yc.upload.web.UploadDo;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Map;

@AutoConfiguration
@ComponentScan("com.yc.upload")
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnProperty(prefix = "upload",name = "enable")
public class UploadAutoConfiguration implements WebMvcConfigurer {

    @Resource
    UploadDo uploadDo;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        final Map<String, String> mapping = uploadDo.getMapping();
        mapping.forEach((webPath,disPath)->{
            registry.addResourceHandler("/" + webPath + "/**")
                    .addResourceLocations("file:" + disPath + "/");
        });
//        registry.addResourceHandler("/upload/**")
//                .addResourceLocations("file:F:\\_文件柜\\133\\组员(李四)\\yc-133\\upload-starter\\src\\main\\resources\\static\\upload/");
    }
}
